package com.jade.bss.web.interceptor;

import com.jade.framework.base.lang.StatusException;
import com.jade.framework.base.util.ResponseUtils;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import sun.misc.Cache;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
/**
 * Created with IntelliJ IDEA.
 * User: jinya
 * Date: 2017/6/27
 * Time: 19:27
 * TEL :17701640710   MAIL:2009_jinya@163.com
 * To change this template use File | Settings | File Templates.
 */
public class ApplicationException implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) {
        ModelAndView mv = new ModelAndView();
        try {
            if(exception instanceof IOException){
                ResponseUtils.writeErrorResult(response, 201,"account_not_existed","服务器异常");
            }else if(exception instanceof SQLException){
                ResponseUtils.writeErrorResult(response, 403,"authentication_required","帐号不存在");
            }else if(exception instanceof org.apache.shiro.authz.UnauthenticatedException){
                ResponseUtils.writeErrorResult(response, 403,"authentication_required","未登录,请重新登录");
            }else if(exception instanceof org.apache.shiro.authc.UnknownAccountException){
                ResponseUtils.writeErrorResult(response, 201,"account_not_existed","帐号不存在");
            }else if(exception instanceof StatusException){
                ResponseUtils.writeErrorResult(response, 201,(StatusException)exception);
            }else if(exception instanceof org.apache.shiro.authc.UnknownAccountException){
                ResponseUtils.writeErrorResult(response, 201,"account_not_existed","帐号不存在");
            }else if(exception instanceof org.apache.shiro.authc.AuthenticationException){
                ResponseUtils.writeErrorResult(response, 201,"account_not_existed","帐号不存在");
            }else{
                ResponseUtils.writeErrorResult(response, 201,exception.getCause().getCause().toString(),"服务器异常");
            }
        } catch (Exception e){}
        return mv;
    }

}
